﻿using System;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;

using Common;
using DAL;

namespace Core.Wapper.CSharpWapper
{
    public class TableWrapper
    {
        private List<ColumnWrapper> m_Columns = null;
        public List<ColumnWrapper> Columns
        {
            get { if (m_Columns == null) { m_Columns = WrapColumns(this.Table.Columns); } return m_Columns; }
        }

        private List<ColumnWrapper> m_PkColumns = null;
        public List<ColumnWrapper> PkColumns
        {
            get { if (m_PkColumns == null) { m_PkColumns = WrapColumns(this.Table.PkColumns); } return m_PkColumns; }
        }

        private List<ColumnWrapper> m_NonPkColumns = null;
        public List<ColumnWrapper> NonPkColumns
        {
            get { if (m_NonPkColumns == null) { m_NonPkColumns = WrapColumns(this.Table.NonPkColumns); } return m_NonPkColumns; }
        }

        private List<ColumnWrapper> m_IdentityColumns = null;
        public List<ColumnWrapper> IdentityColumns
        {
            get { if (m_IdentityColumns == null) { m_IdentityColumns = WrapColumns(this.Table.IdentityColumns); } return m_IdentityColumns; }
        }

        //For Insert
        private List<ColumnWrapper> m_NonIdentityColumns = null;
        public List<ColumnWrapper> NonIdentityColumns
        {
            get { if (m_NonIdentityColumns == null) { m_NonIdentityColumns = WrapColumns(this.Table.NonIdentityColumns); } return m_NonIdentityColumns; }
        }

        //For Update
        private List<ColumnWrapper> m_UpdateColumns = null;
        public List<ColumnWrapper> UpdateColumns
        {
            get { if (m_UpdateColumns == null) { m_UpdateColumns = WrapColumns(this.Table.UpdateColumns); } return m_UpdateColumns; }
        }

        public bool HasIdentityColumn
        {
            get { return IdentityColumns.Count > 0; }
        }

        public bool IsTable //Not View
        {
            get { return this.Table.IsTable; }
        }

        public TableInfo Table = null;

        private DbCommandBuilder m_CommandBuilder = null;

        public TableWrapper(SchemaHeler schemaHeler, string tableName, string[] tableRestriction)
        {
            this.Table = new TableInfo(schemaHeler, tableName, tableRestriction);
            this.m_CommandBuilder = this.Table.SchemaHeler.DBFactory.CreateCommandBuilder();
        }

        public string EntityName
        {
            get
            {
                return NameUltility.ToPascal(this.Table.Name);
            }
        }

        public string QualifiedTableName
        {
            get
            {
                return string.Concat(this.m_CommandBuilder.QuoteIdentifier(this.Table.Table_Schema), "."
                    , this.m_CommandBuilder.QuoteIdentifier(this.Table.Name));
            }
        }

        public List<ColumnWrapper> WrapColumns(List<ColumnInfo> columns)
        {
            var wappedColumns = new List<ColumnWrapper>();
            for (int i = 0; i < columns.Count; i++)
            {
                wappedColumns.Add(new ColumnWrapper(columns[i],this));
            }
            return wappedColumns;
        }
    }
}
